package com.ruoyi.security.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.security.domain.AfDeviceInfo;
import com.ruoyi.security.mapper.AfDeviceInfoMapper;
import com.ruoyi.security.service.IAfDeviceInfoService;
import com.ruoyi.system.api.LinkFileServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 设备信息Service业务层处理
 *
 * @author cjie
 * @date 2021-07-24
 */
@Service
public class AfDeviceInfoServiceImpl extends LinkFileServiceImpl implements IAfDeviceInfoService {
    @Autowired
    private AfDeviceInfoMapper afDeviceInfoMapper;

    /**
     * 查询设备信息
     *
     * @param id 设备信息ID
     * @return 设备信息
     */
    @Override
    public AfDeviceInfo selectAfDeviceInfoById(String id) {
        return afDeviceInfoMapper.selectAfDeviceInfoById(id);
    }

    /**
     * 查询设备信息列表
     *
     * @param afDeviceInfo 设备信息
     * @return 设备信息
     */
    @Override
    public List<AfDeviceInfo> selectAfDeviceInfoList(AfDeviceInfo afDeviceInfo) {
        return afDeviceInfoMapper.selectAfDeviceInfoList(afDeviceInfo);
    }

    /**
     * 新增设备信息
     *
     * @param afDeviceInfo 设备信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertAfDeviceInfo(AfDeviceInfo afDeviceInfo) {
        afDeviceInfo.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        afDeviceInfo.setCreateTime(DateUtils.getNowDate());
        afDeviceInfo.setCreateBy(SecurityUtils.getUsername());

        linkFile(afDeviceInfo.getFileIdList(), afDeviceInfo.getId());
        return afDeviceInfoMapper.insertAfDeviceInfo(afDeviceInfo);
    }

    /**
     * 修改设备信息
     *
     * @param afDeviceInfo 设备信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateAfDeviceInfo(AfDeviceInfo afDeviceInfo) {
        afDeviceInfo.setUpdateTime(DateUtils.getNowDate());
        afDeviceInfo.setUpdateBy(SecurityUtils.getUsername());

        linkFile(afDeviceInfo.getFileIdList(), afDeviceInfo.getId());
        return afDeviceInfoMapper.updateAfDeviceInfo(afDeviceInfo);
    }

    /**
     * 批量删除设备信息
     *
     * @param ids 需要删除的设备信息ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteAfDeviceInfoByIds(String[] ids) {
        deleteLinkFile(ids);
        Arrays.stream(ids).forEach(deviceId->{
            afDeviceInfoMapper.deletePermissionDeviceByDeviceId(deviceId);
        });
        return afDeviceInfoMapper.deleteAfDeviceInfoByIds(ids);
    }

    /**
     * 删除设备信息信息
     *
     * @param id 设备信息ID
     * @return 结果
     */
    @Override
    public int deleteAfDeviceInfoById(String id) {
        return afDeviceInfoMapper.deleteAfDeviceInfoById(id);
    }
}
